<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Mail;

class RegController extends Controller
{
    // 显示注册页面
    public function register()
    {
       return view('home.Register');
    }
    // 进行注册
    public function doRegister(Request $request)
    {
        // 验证表单提交信息
        $this->validate($request, [
            'username' => 'required|regex:/^\S{8,18}$/',
            'password'=>'required|regex:/^\S{6,20}$/|same:repassword',
            'email'=>'required',
            'checked'=>'required',
        ],[
            'username.required'=>'用户名不能为空',
            'username.regex'=>'用户名格式不正确',
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码格式不正确',
            'password.same'=>'两次密码不一致',
            'email.required'=>'邮箱不能为空',
            'checked.required'=>'协议同意',
        ]);

      
        // 得到数据
       $data =  $request->except(['_token','repassword','checked']);
        // 加密密码
       $data['password'] = Hash::make($data['password']);
       // 插入数据库

       $id  = DB::table('shop_users')->insertGetId($data);
       // 给每个id分配随机的唯一数字
       $tid =time() + rand(100000,999999);
       // 判断
       if($id){
            // 邮件
            Mail::send('home.ji', ['data'=>$data,'id'=>$id,'tid'=>$tid], function ($m) use ($data){
                $m->to($data['email'], $data['username'])->subject('商城邮件发送提醒');
        }); 
            return redirect('/')->with('info','注册成功');
        }else{
            return back();
        }

    }
    // ajax 进行用户名验证 
    public function checkUser(Request $request)
    {
            $user =  $request->input();
            $data = DB::table('shop_users')->where('username',$user)->get();
            if($data){
                return '1';
            }else{
                return '0';
            }

            return view('/home/Register',['data'=>$data]);
    }
    // 邮箱激活测试
    public function ji(Request $request)
    {
        $id = $request->input('id');
        // 更新状态
        $res = DB::table('shop_users')->where('id',$id)->update(['status'=>1]);
       if($res){
            return redirect('/home/login')->with('info','激活成功,请登录');
       }else{
            return back();
       }
    }


    // 登录页面显示
    public function login()
    {
        return view('home.login');
    }

    // 登录
    public function dologin(Request $request)
    {
        // 验证表单提交信息
        $this->validate($request, [
            'username' => 'required',
            'password'=>'required',
        ],[
            'username.required'=>'请输入用户名',
            'password.required'=>'密码不能为空',
            // 'password.required'=>'',
        ]);
        $data = $request->except('_token');
        // 查询用户名是否存在
        $res = DB::table('shop_users')->where('username','=',$data['username'])->first();
        // dd($res);
        if(!$res){
            return back()->with('info','用户名不存在,请重新输入');
        }else{
            // 判断与之对应的密码是否正确
            if (Hash::check($data['password'], $res->password)) {
                session(['id'=>$res->id]);
                // dd(session('id'));
                return redirect('/'); 
            }
            else{
                return back()->with('info','密码输入错误,请重新输入');
            }
        }

    }

    // 找回密码
    public function forget()
    {
        return view('home.forget');
    }

    public function doforget(Request $request)
    {
       $user = $request->input('email');
       // dd($user);
       $data = DB::table('shop_users')->where('email',$user)->first();
       // dd($data);
       if(!$data){
            return back()->with('info','邮箱不存在');
       }else{
            $flag = Mail::send('home.yanZhen', ['data' => $data], function ($m) use ($data) {

            $m->to($data->email, $data->username)->subject('密码找回邮件提醒!');
        });
           if($flag ==1){
            echo '发送成功...返回邮箱查看';
           }else{
            echo  '发送失败...请返回重新发送';
           }
        }
    }
    

    // 页面的重置
    public function reset(Request $request)
    {
        return view('home.reset',['id'=>$request->input('id')]);
    }

    public function doreset(Request $request)
    {
        // 验证表单提交信息
        $this->validate($request, [
            'password'=>'required|regex:/^\S{6,20}$/|same:repassword',

        ],[
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码格式不正确',
            'password.same'=>'两次密码不一致',
        ]);
        

       $data['password'] = Hash::make($request->input('password'));
       // dd($data['password']);

        $data = DB::table('shop_users')->where('id',$request->input('id'))->update($data);

        if(!$data){
            return back()->with('info','密码更新失败');
        }else{
            return redirect('/home/login')->with('info','密码更新成功');
        }
    }

    // 天气预报
    public function dowea()
    {
        $ch = curl_init();
        $url = 'http://apis.baidu.com/heweather/weather/free?city=beijing';
        $header = array(
            'apikey: d4a0ddd45d4d271ee216df24c4e24e1a',
        );
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        
        $arr = [];
        $arrs = [];
        $res = json_decode($res,true);
        // dd($res);
        $arr[]=$res["HeWeather data service 3.0"][0]["daily_forecast"];
        // dd($arr);
        foreach($arr as $k=>$v){
            foreach($arr[$k] as $kk=>$vv){
                    $arrs[$kk]['date'] = $vv['date'];
                    $arrs[$kk]['cond'] = $vv['cond'];
                    $arrs[$kk]['tmp'] = $vv['tmp'];
                }
        }
        // dd($arrs);

        return view('/home/weather',['arrs'=>$arrs]);
    }

    // 退出登录
    public function delUsername()
    {
        session()->forget('id');
        return redirect('/');
    }
    
}
